package com.example.zzl.adapter.HomeFragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.adapter.RecyclerViewBaseAdapter;
import com.example.zzl.databinding.HomeInfoListItemBinding;
import com.example.zzl.model.bean.Home;
import com.example.zzl.utils.specification.VolleySingleton;

import java.util.List;

/**
 * Created by ligg on 2017/2/11.
 */

public class InfoListAdapter extends RecyclerViewBaseAdapter<Home.News, InfoListAdapter.ViewHolder>{

    public InfoListAdapter(Context context, List<Home.News> list) {
        super(context, list);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        HomeInfoListItemBinding db = DataBindingUtil.inflate(inflater, R.layout.home_info_list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(db.getRoot());
        viewHolder.db = db;
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        Home.News news = list.get(position);
//        Log.e("NewsAdapter", "onBindViewHolder: " + news.getIsGood());
        holder.db.setNews(news);
        holder.db.include.twoPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        final ImageView iv = holder.db.include.iv;
        iv.setImageBitmap(null);
        iv.setTag(news.MainImage);
        VolleySingleton.volleySingleton.getImageLoader().get(news.MainImage, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (iv.getTag().equals(response.getRequestUrl())) {
                    iv.setImageBitmap(response.getBitmap());
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public HomeInfoListItemBinding db;

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}
